#include <stdio.h>
#include <conio.h>
#define MAXN 100
struct phanso
{
	int tu,mau;
};
int ucln(int x,int y)
{
	if (y == 0) return x; 
	else return ucln(y,x%y);
}
phanso rutgon(phanso p)
{
	int uc=ucln(p.tu,p.mau);
	p.tu=p.tu/uc;
	p.mau=p.mau/uc;
	return p;
}
/*void sapxep(phanso a[],int n)
{
	phanso tmp;
	int i,j;
	for (i=0;i<n-1;i++)
		for (j=i+1;j<n;j++)
			if (a[i].tu*a[j].mau<a[i].mau*a[j].tu)
			{
				tmp=a[i];
				a[i]=a[j];
				a[j]=tmp;
			}
}*/ //phan nay cac ban bo sung them sap xep voi so <0
int dem(phanso a[],int n)
{
    int i,dem=0;
    for(i=0;i<n;i++)
    if(((a[i].tu<0)&&(a[i].mau>0))||((a[i].tu>0)&&(a[i].mau<0)))
    dem=dem+1;
    return dem;
}
main()
{
      phanso a[100];
      int n,i;
      printf("Nhap so luong phan so: ");
      scanf("%d",&n);
      for (i=0;i<n;i++)
          {
            printf("Nhap phan so thu %d: ",i);
            scanf("%d%d",&a[i].tu,&a[i].mau);
          }
      //Rut gon cac phan so
      for (i=0;i<n;i++)
          a[i]=rutgon(a[i]);
          
      printf("Danh sach cac phan so vua nhap vao sau khi rut gon:\n");
      for (i=0;i<n;i++)
          printf("%d/%d ",a[i].tu,a[i].mau);
      
      printf("\nDanh sach cac phan so vua nhap vao sau khi sap xep giam dan: \n");
      //sapxep(a,n);
      for (i=0;i<n;i++)
          printf("%d/%d ",a[i].tu,a[i].mau);
      printf("\n So luong phan so am la %d",dem(a,n));
      getch();
}
